package cn.ac.iscas.action.thesis;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.ac.iscas.bean.PageView;
import cn.ac.iscas.bean.thesis.Thesis;
import cn.ac.iscas.service.thesis.IThesisService;

@Controller @Scope("prototype")
public class ThesisAction {
	private ThesisQuery thesisQuery;

	
	

	/***********KE*************/
	@Resource IThesisService thesisService;
	private int page;
    
	public IThesisService getThesisService() {
		return thesisService;
	}

	public void setThesisService(IThesisService thesisService) {
		this.thesisService = thesisService;
	}
	/***********KE*************/
	
	
	
	
	
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	public String queryPage(){
		return "query";
	}
	public String typeIn()
	{
		return "add";
	}
	public String list()
	{
		page = (0==page)?1:page;
		PageView<Thesis> pageView = new PageView<Thesis>(7, page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(thesisService.getScrollData(Thesis.class,pageView.getFirstResult(),pageView.getMaxresult(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "query";
	}
	public String query(){
		if(page<=0)page=1;
		StringBuffer jpql = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		if(thesisQuery.getPaperTitle()!=null && !"".equals(thesisQuery.getPaperTitle())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.paperTitle like ?");
			params.add("%"+ thesisQuery.getPaperTitle()+ "%");
		}
		if(thesisQuery.getAuthor()!=null && !"".equals(thesisQuery.getAuthor())){
			if(params.size()>0) jpql.append(" and ");
			System.out.println(thesisQuery.getAuthor()+"name");
			jpql.append(" o.author like ?");
			params.add("%"+thesisQuery.getAuthor()+ "%");
		}
		if(thesisQuery.getTutor()!=null && !"".equals(thesisQuery.getTutor())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.tutor like ?");
			params.add("%"+thesisQuery.getTutor()+ "%");
		}
		
		if(thesisQuery.getGraduateYear()!=null && !"".equals(thesisQuery.getGraduateYear())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.graduateYear = ?");
			params.add(thesisQuery.getGraduateYear());
		}
		
		if(thesisQuery.getThesisType()!=null &&!"".equals(thesisQuery.getThesisType()))
		{
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.thesisType = ?");
			params.add(thesisQuery.getThesisType());
		}
		if(thesisQuery.getKeyWords()!=null &&!"".equals(thesisQuery.getKeyWords()))
		{
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.keyWords like ?");
			params.add("%"+thesisQuery.getKeyWords()+"%");
		}
		PageView<Thesis> pageView=new PageView<Thesis>(7,page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(thesisService.getScrollData(Thesis.class,pageView.getFirstResult(), 
				pageView.getMaxresult(), jpql.toString(), params.toArray(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "query" ;
	}
    public String test() throws IOException
    {
    	ActionContext ctx = ActionContext.getContext();     
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		response.setContentType("text/html;charset=utf-8");
	    HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println(request.getParameter("name"));
		out.println(request.getParameter("name") + ",服务器时间：" + new Date()
		    		+ ",请校对!");
		out.flush();
		out.close();
		return null;
	}
	
    public ThesisQuery getThesisQuery() {
		return thesisQuery;
	}

	public void setThesisQuery(ThesisQuery thesisQuery) {
		this.thesisQuery = thesisQuery;
	}
}
